package app.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import app.ui.MakeDataFileUi;

public class MakeDataFileAction implements ActionListener {

	private MakeDataFileUi mPanel;

	public MakeDataFileAction(MakeDataFileUi mPanel) {
		this.mPanel = mPanel;
	}

	public void actionPerformed(ActionEvent e) {
		JButton jButton = (JButton) e.getSource();
		String[] btnNames = mPanel.getBtnTitles();
		int i = 0;
		for (; i < btnNames.length; i++) {
			if (jButton.getName().equals(btnNames[i])) {
				break;
			}
		}

		JComboBox<String> jComboBox = mPanel.getjComboBox();
		int selectedTypeIndex = jComboBox.getSelectedIndex();

		switch (i) {
		case 1: // Clear
			clearBtnAction(jButton);
			break;
		case 2: // Add
			switch (selectedTypeIndex) {
			case 0: // add question
				addQuestionAction(jButton);
				break;
			case 1: // add answer
				addAnswersAction(jButton);
				break;
			case 2: // add tape script
				addTS(jButton);
				break;
			}
			jButton.setEnabled(false); // add only one time
			mPanel.getjTextFields()[1].setEditable(false);
			break;
		case 3: // Format
			String tmpStr = null;
			String[] texts = mPanel.getTextContentAction().getTextContents();
			int num = -1;
			if(texts == null || texts.length < 1) {
				selectedTypeIndex = -1;
			}
			switch (selectedTypeIndex) {
			case 0: // add question
				tmpStr = mPanel.getExHelper().formatQuestion(texts);
				if (tmpStr != null) {
					mPanel.getjTextArea().setText(tmpStr);
				}

				num = texts.length;
				if ((num % 5) == 0) {
					mPanel.getjButtons()[2].setEnabled(true);
					num = num / 5;
				}
				mPanel.getjTextFields()[2].setText(String.valueOf(num));

				break;
			case 1: // add answer
				tmpStr = mPanel.getExHelper().formatAnswer(texts);
				if (tmpStr != null) {
					mPanel.getjTextArea().setText(tmpStr);
				}
				num = texts.length;
				if (num == mPanel.getExHelper().getAmtQue() || num == 1) {
					mPanel.getjButtons()[2].setEnabled(true);
				}
				break;
			case 2: // add tape script
				mPanel.getjButtons()[2].setEnabled(true);
				int index = Integer.parseInt(mPanel.getjTextFields()[1].getText());
				if(index < 1) {
					mPanel.getjTextFields()[1].setText("1");
				}
				
				num = 1;
				break;
			}
			// set the value of input index question
			if (num == 1) {
				mPanel.getjTextFields()[1].setEditable(true);
			} else {
				mPanel.getjTextFields()[1].setText("-1");
				mPanel.getjTextFields()[1].setEditable(false);
			}
			break;
		case 5: // Close
			closeBtnAction(jButton);
			break;
		default:
			break;
		}
	}

	private void addAnswersAction(JButton jButton) {
		String[] answers = mPanel.getTextContentAction().getTextContents();
		int index = Integer.parseInt(mPanel.getjTextFields()[1].getText());
		index = mPanel.getExHelper().writeCorrectAnswers(answers, index);
		mPanel.getjTextFields()[1].setText(String.valueOf(index));
	}

	private void addTS(JButton jButton) {
		int index = Integer.parseInt(mPanel.getjTextFields()[1].getText());
		String str = mPanel.getjTextArea().getText();
		try {
			index = mPanel.getExHelper().writeTapeScrip(str, index);
			mPanel.getjTextFields()[1].setText(String.valueOf(index));
		} catch (Exception e) {
			JOptionPane.showMessageDialog(mPanel, e.getMessage());
		}
	}

	private void addQuestionAction(JButton jButton) {
		TextContentAction tAction = mPanel.getTextContentAction();
		int num = tAction.getTextContents().length;
		mPanel.getExHelper().answersVal = new String[4];
		for (int i = 0; i < num; i++) {
			if ((i % 5) == 0) {
				mPanel.getExHelper().questionVal = tAction.getTextContents()[i];
			} else {
				mPanel.getExHelper().answersVal[i % 5 - 1] =
						tAction.getTextContents()[i];
			}

			if (((i + 1) % 5) == 0) {
				int index = Integer.parseInt(mPanel.getjTextFields()[1].getText());
				try {
					index = mPanel.getExHelper().writeQueAns(index);
					mPanel.getExHelper().answersVal = new String[4];
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		jButton.setEnabled(false);
	}

	private void clearBtnAction(JButton jButton) {
		mPanel.getjTextArea().setText("");
		mPanel.getjTextArea().requestFocus();
	}

	private void closeBtnAction(JButton jButton) {
		mPanel.close(false);
	}

}
